package listtest;

import java.util.Arrays;

public class MyArrayList implements  IList{

    public int[] array;//存放顺序表
    public int useSize;//顺序表长度

    public static final int DEFAULT_CAPACITY = 10;//设置数组默认长度为10，方便之后进行修改

    public MyArrayList(){
        this.array = new int[DEFAULT_CAPACITY];
    }


    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.array[this.useSize] = data;
        useSize++;
    }

    public void grow(){
        this.array = Arrays.copyOf(this.array,2*this.array.length);
    }


    @Override
    public boolean isFull() {
        if(useSize == array.length)
            return true;
        return false;
    }

    private void checkPos(int pos) throws Poslllegal{//得先检查pos的位置是否正确
        if(pos<0 || pos>array.length){
            throw new Poslllegal("pos的位置不合法");
        }
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
            if(isFull()){
                grow();
            }
            for (int i = useSize-1; i >= pos ; i--) {
                array[i+1] = array[i];
            }
            array[pos] = data;
            useSize++;
        }catch (Poslllegal e){
            System.out.println("插入元素pos的位置不合法");
            e.printStackTrace();
        }
    }

    @Override
    public boolean contains(int toFind) { //判断是否包含某个元素
        for (int i = 0; i < useSize; i++) {
            if (array[i] == toFind)
                return true;
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) { //查找某个元素
        for (int i = 0; i < useSize; i++) {
            if (array[i] == toFind)
                return i;
        }
        return 0;
    }

    private void checkPos2(int pos) throws Poslllegal{
        if (pos < 0 || pos >= useSize)
            throw new Poslllegal("Pos的位置不合法！");
    }

    public boolean isEmpty(){
        return useSize == 0;
    }

    private void checkEmpty(){
        if (isEmpty()){
            throw new EmptyException("顺序表是空的！");
        }
    }

    @Override
    public int get(int pos) { //获取某个位置的元素
        try{
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (Poslllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int value) { //修改pos位的元素
        try{
            checkEmpty();
            checkPos2(pos);
            array[pos] = value;
        }catch (Poslllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }

    }

    @Override
    public void remove(int toRemove) { //删除第一次出现的元素
        try{
            checkEmpty();
            int pos = indexOf(toRemove);
            if (pos == -1)
                return;
            for (int i = pos; i < useSize-1; i++) {
                array[i] = array[i+1];
            }
            useSize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.useSize;
    }

    @Override
    public void clear() {
        useSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.useSize; i++) {
            System.out.print(array[i]+" ");
        }
    }
}
